#include "settingwidget.h"
#include <QLabel>
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QHBoxLayout>
#include "custommessagebox.h"
#include "leiliconfig.h"
#include "toast.h"
#include <QLine>
#include <QFileDialog>



SettingWidget::SettingWidget(QWidget *parent) : QWidget(parent)
{
    setupUI();
    initConfig();
    createConnect();
}

void SettingWidget::setupUI()
{
    int leftSpace = 240;
    auto addSpace = [=](QHBoxLayout *p){
        p->addSpacing(leftSpace);
    };
    QVBoxLayout *vMain = new QVBoxLayout(this);


    QVBoxLayout *vSave = new QVBoxLayout(this);

    QHBoxLayout *vHSave1 = new QHBoxLayout(this);
    auto *label = new QLabel(this);
    label->setText("栅格地图&pcd保存设置:");
    vHSave1->addWidget(label);
    vHSave1->addStretch();



    QHBoxLayout *vHSave2 = new QHBoxLayout(this);

    label = new QLabel(this);
    label->setText("保存目录(默认将生成的栅格地图(.yaml,.pgm)及三维点云文件(.pcd)保存在文件夹下)");
    addSpace(vHSave2);
    vHSave2->addWidget(label);
    vHSave2->addStretch();


    QHBoxLayout *vHSave3= new QHBoxLayout(this);
    m_savePath = new QLabel(this);
    m_savePath->setText("./");


    m_btnChangeSavePath = new QPushButton(this);
    m_btnChangeSavePath->setText("更改目录");

    addSpace(vHSave3);
    vHSave3->addWidget(m_savePath);
    vHSave3->addSpacing(48);
    vHSave3->addWidget(m_btnChangeSavePath);
    vHSave3->addStretch();


    vSave->addLayout(vHSave1);
    vSave->addLayout(vHSave2);
    vSave->addLayout(vHSave3);


    //QLine * line = new QLine();



    QVBoxLayout *vL = new QVBoxLayout(this);

    QHBoxLayout *vH0 =  new QHBoxLayout(this);
    label = new QLabel(this);
    label->setText("路径规划参数设置:");
    vH0->addWidget(label);
    vH0->addStretch();

    QHBoxLayout *vH1 =  new QHBoxLayout(this);
    QLabel *p = new QLabel(this);
    p->setText("机器人数量:");

    m_robotNum = new QSpinBox(this);
    m_robotNum->setMinimum(1);

    addSpace(vH1);
    vH1->addWidget(p);
    vH1->addSpacing(48);
    vH1->addWidget(m_robotNum);
    vH1->addStretch();


    QHBoxLayout *vH2 =  new QHBoxLayout(this);
    p = new QLabel(this);
    p->setText("研磨盘直径(m):");

    m_grindingPanDiameter = new QDoubleSpinBox(this);

    addSpace(vH2);
    vH2->addWidget(p);
    vH2->addSpacing(48);
    vH2->addWidget(m_grindingPanDiameter);
    vH2->addStretch();

    QHBoxLayout *vH3 =  new QHBoxLayout(this);
    p = new QLabel(this);
    p->setText("重叠宽度(m):");

    m_overlapWidth = new QDoubleSpinBox(this);

    addSpace(vH3);
    vH3->addWidget(p);
    vH3->addSpacing(48);
    vH3->addWidget(m_overlapWidth);
    vH3->addStretch();

    QHBoxLayout *vH4 =  new QHBoxLayout(this);
    m_btnConfirm = new  QPushButton(this);
    m_btnConfirm->setText("确认");
    vH4->addStretch();
    vH4->addWidget(m_btnConfirm);

    vL->addLayout(vH0);
    vL->addLayout(vH1);
    vL->addLayout(vH2);
    vL->addLayout(vH3);
    vL->addStretch();
    //vL->addLayout(vH4);



    vMain->addLayout(vSave);

    QFrame *line = new QFrame(this);
    line->setFixedHeight(2);
    line->setFrameShape(QFrame::HLine); // 水平分割线
    line->setFrameShadow(QFrame::Sunken); // 设置阴影效果

    vMain->addWidget(line);
    vMain->addLayout(vL);
    vMain->addLayout(vH4);
}

void SettingWidget::initConfig()
{
    m_robotNum->setValue(LeiliCfg->getRobotNum());
    m_grindingPanDiameter->setValue(LeiliCfg->getGrindingPanDiameter());
    m_overlapWidth->setValue(LeiliCfg->getOverlapWidth());
    m_savePath->setText(LeiliCfg->getSavePath());
}

void SettingWidget::createConnect()
{
    connect(m_btnConfirm, &QPushButton::clicked, this, &SettingWidget::onConfirmClick);
    connect(m_btnChangeSavePath, &QPushButton::clicked, this, &SettingWidget::onChangeSavePath);
}

void SettingWidget::onConfirmClick()
{
    int robotNum = m_robotNum->value();
    if(robotNum <= 0)
    {

        return;
    }

    double dia = m_grindingPanDiameter->value();
    if(dia  <=0)
    {
        CustomMessageBox::warning(nullptr, "提示","机研磨盘直径必须大于0");
        return;
    }

    double width = m_overlapWidth->value();
    if(width <= 0)
    {
        CustomMessageBox::warning(nullptr, "提示","重叠宽度必须大于0");
        return;
    }

    if(LeiliCfg->getRobotNum() != robotNum){
        //CustomMessageBox::warning(nullptr, "提示","确定要修改机器人数量吗");
        LeiliCfg->setRobotNum(robotNum);
    }

    LeiliCfg->setSavePath(m_savePath->text());
    LeiliCfg->setGrindingPanDiameter(dia);
    LeiliCfg->setOverlapWidth(width);
    LeiliCfg->sync();


    Toast::toast("succImg","保存成功",QPoint(this->pos().x() + this->width()/2,this->pos().y() + this->height()/2),nullptr);
}

void SettingWidget::onChangeSavePath()
{
    QString path = QFileDialog::getExistingDirectory(this, "选择pgm(yaml)/pcd文件存储目录", LeiliCfg->getSavePath(),QFileDialog::DontUseNativeDialog);
    if (path == "")
    {
        return;
    }

    m_savePath->setText(path);
}
